import {
	storage
} from "@/utils/base"
import {
	GetMember
} from '@/api/account/account.js'

// 是否登录
const isLogin = storage.get("isLogin")

// 检测token是否过期
const checkTokenExpiration = () => {
	if (!isLogin) {
		uni.redirectTo({
			url: '/pages/account/login/login'
		})
	}
	const nowTime = new Date().getTime()
	const expiresTime = storage.get('expiresTime') || 0
	return Promise.resolve(nowTime - expiresTime)
}


// 针对token是否过期 的处理方法
const handleTokenExpiration = (timeout) => {
	checkTokenExpiration().then(res => {
		// res大于0 --->token过期，到登录页
		// res小于0 --->token未过期，到指定页
		setTimeout(() => {
			if (res <= 0) {
				// token未过期
				uni.switchTab({
					url: '/pages/parent/parent-home/parent-home'
				})
			} else {
				// token过期
				uni.redirectTo({
					url: '/pages/account/login/login'
				})
			}
		}, timeout)
	})
}

// 检测账号所在地区、学校是否有使用睿吃系统
// 检测帐号孩子信息是否有完善
export const checkHasSystemAndChildInfo = () => {
	return new Promise(async (resolve, reject) => {
		const result = await GetMember()
		const ret = {
			hasSystem: false,
			hasChildrenInfo: false
		}
		if (result.state === 1) {
			const data = result.attribute
			const studentList=data.filter(v=>v.memberType === 0)
			if(studentList.length) {
				ret.hasChildrenInfo =true
				ret.hasSystem = studentList[0].schoolId != 0 
			}
			storage.set('accountState', ret)
			resolve(result)
		} else {
			storage.set('accountState', ret)
			reject(result)
		}
	})
}

// 处理登录成功后跳转
export const handleLoginSuccess = () => {
	uni.switchTab({
		url: '/pages/parent/parent-home/parent-home'
	});
}
